<template>
  <div class="content login">
    <THeader :active="this.$route.path"></THeader>
    <!--登录成功页面模块-->
    <el-row style="padding-top: 100px">
      <el-col :span="8" :offset="8">
        <el-card class="box-card">
          <div slot="header" class="clearfix">
            <h2 style="margin: 0">用户登录</h2>
          </div>
          <el-form :model="loginForm" label-position="top" :rules="rules" ref="ruleForm">
            <el-form-item label="用户名" prop="username">
              <el-input v-model="loginForm.username" auto-complete="true"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="loginForm.password" type="password"></el-input>
            </el-form-item>
            <div>
              <el-button style="width: 100%" size="large" type="primary" @click="loginAction">登录</el-button>
            </div>
            <div>
              <el-button type="text" :disabled="true">修改密码</el-button>
              <el-button type="text" @click="routeTo('/register')">注册</el-button>
            </div>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { loginServer } from "../../api/user";
import { mapActions, mapMutations } from "vuex";

export default {
  name: "login",
  data() {
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          { required: true, message: "用户名不能少", trigger: "blur" },
          { min: 6, max: 16, message: "用户名在6到16位之间", trigger: "blur" }
        ],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }]
      }
    };
  },
  computed: {},

  methods: {
    ...mapActions(["setInterceptors"]),
    ...mapMutations(["setUserState"]),
    routeTo(path) {
      this.$router.push(path);
    },
    loginAction() {
      //登录功能
      this.$refs["ruleForm"].validate(valid => {
        if (valid) {
          const { username, password } = this.loginForm;
          loginServer("post", {
            user: { username, password }
          }).then(
            suc => {
              const { code, data, message } = suc.data;
              if (code) {
                this.setInterceptors(data.token);
                this.$message({ type: "success", message });
                this.setUserState({
                  isLogin: true,
                  userData: {
                    userId: data.userId,
                    username: data.username,
                    authority: data.authority
                  }
                });
                this.routeTo("/");
              } else {
                this.setUserState({
                  isLogin: false
                });
                this.$message({ type: "error", message });
              }
            },
            err => {
              this.setUserState({ isLogin: false });
              this.$message({ type: "error", message: err.message });
            }
          );
        }
      });
    }
  }
};
</script>
